package com.earogya.springsecurity.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.provisioning.UserDetailsManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import com.earogya.springsecurity.security.AddUsersToUserGroupsInterface;
import com.earogya.springsecurity.security.ChangePasswordInterface;
import com.earogya.springsecurity.security.CreateUsersInterface;
import com.earogya.springsecurity.service.AdminService;

/**
 * Used to service account requests.
 * 
 * @author Mularien
 */
@Controller
public class AdminController extends BaseController {
	// Ch 4 start and end
	@Autowired
	private CreateUsersInterface createUsersDao;
	
	@Autowired
	private AddUsersToUserGroupsInterface addUsersToUserGroupsDao;
	//private ChangePasswordInterface changePasswordDao;
	// Ch 4 UserDetailsManager exercise
	//@Autowired
	//private UserDetailsManager userDetailsManager;
	
	@RequestMapping(value="/admin/createUsers.do",method=RequestMethod.GET)
	public void showCreateUsersPage() {		
	}
		
	@RequestMapping(value="/admin/createUsers.do",method=RequestMethod.POST)
	public String submitCreatePage(@RequestParam("username") String username,@RequestParam("name") String name,@RequestParam("password") String password,@RequestParam("email") String email,@RequestParam("department") String department) {
		try{
		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

		/*String username = principal.toString();
		if (principal instanceof UserDetails) {
		  username = ((UserDetails)principal).getUsername();
		}*/
		//createUsersDao.createUser("urekas", "urekas");
		createUsersDao.createUser(username, name, password, email, department);
		//SecurityContextHolder.clearContext();
		
		return "redirect:/";
		}catch(DuplicateKeyException e){
			return "Username already exists";
		}
	}
	
	@RequestMapping(value="/admin/add_users_to_user_groups.do",method=RequestMethod.GET)
	public void showAddUsersToUserGroupsPage() {		
	}
	
	@RequestMapping(value="/admin/add_users_to_user_groups.do",method=RequestMethod.POST)
	public String submitAddUsersToUserGroupsPage(@RequestParam("username") String username,@RequestParam("userGroup") int userGroup) {		
		
		addUsersToUserGroupsDao.addUsersToUserGroups(username, userGroup);

		
		return "redirect:/";
		
	}

	// Ch 4 UserDetailsManager version
	/*@RequestMapping(value="/profile/changePassword.do",method=RequestMethod.POST)
	public String submitChangePasswordPage(@RequestParam("oldpassword") String oldPassword, 
			@RequestParam("password") String newPassword) {
		userDetailsManager.changePassword(oldPassword, newPassword);
		SecurityContextHolder.clearContext();
		
		return "redirect:home.do";
	}*/
	
}
